<!DOCTYPE html>
<html lang="en" class="no-js" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>客户管理</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<!--顶部搜索框-->
<form class="layui-form" action="/custom/queryAll" style="margin-top: 10px">
    <div class="layui-inline">
        <label class="layui-form-label">联系人:</label>
        <div class="layui-input-inline">
            <input type="text" name="contact" autocomplete="off" placeholder="联系人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">申请人:</label>
        <div class="layui-input-inline">
            <input type="text" name="applicant" autocomplete="off" placeholder="申请人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">修改人:</label>
        <div class="layui-input-inline">
            <input type="text" name="updateBy" autocomplete="off" placeholder="修改人" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            <button class="layui-btn" lay-submit lay-filter="searchFilter">搜索</button>
            <button type="reset" class="layui-btn layui-btn-normal" id="myButton">重置</button>
        </div>
    </div>
</form>
<!--客户列表显示-->
<table id="customTable" lay-filter="customFilter"></table>
<!--客户更新form-->
<form id="customUpdateForm" class="layui-form" lay-filter="customUpdateFilter" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <input hidden name="customId">
    <div class="layui-form-item">
        <label class="layui-form-label">代理人</label>
        <div class="layui-input-inline">
            <input hidden name="proxyId" id="updateUserTableSelectHidden">
            <input type="text" readonly id="updateUserTableSelect" name="userName" lay-verify="required" placeholder=""
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">申请人</label>
        <div class="layui-input-block">
            <input type="text" name="applicant" lay-verify="required" placeholder="" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="tel" name="phone" lay-verify="required|phone" placeholder="" autocomplete="off" class="layui-input">
        </div>
        <label class="layui-form-label">联系人</label>
        <div class="layui-input-inline">
            <input type="text" name="contact" lay-verify="required" placeholder="" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱地址</label>
        <div class="layui-input-block">
            <input type="email" name="email" lay-verify="required|email" placeholder="请输入邮箱地址" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">邮寄地址</label>
        <div class="layui-input-block">
            <textarea name="customAddress" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea type="text" name="remark" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="customUpdateFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>
<!--客户添加form-->
<form id="customAddForm" class="layui-form" style="display: none"> <!-- 提示：如果你不想用form，你可以换成div等任何一个普通元素 -->
    <div class="layui-form-item">
        <label class="layui-form-label">代理人</label>
        <div class="layui-input-block">
            <input hidden name="proxyId" id="addUserTableSelectHidden">
            <input type="text" readonly id="addUserTableSelect" lay-verify="required" placeholder="请选择代理人"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">申请人</label>
        <div class="layui-input-block">
            <input type="text" name="applicant" lay-verify="required" placeholder="请输入申请人" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号</label>
        <div class="layui-input-inline">
            <input type="tel" name="phone" lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off"
                   class="layui-input">
        </div>
        <label class="layui-form-label">联系人</label>
        <div class="layui-input-inline">
            <input type="text" name="contact" lay-verify="required" placeholder="请输入联系人" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱地址</label>
        <div class="layui-input-block">
            <input type="email" name="email" lay-verify="required|email" placeholder="请输入邮箱地址" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">邮寄地址</label>
        <div class="layui-input-block">
            <textarea name="customAddress" placeholder="请输入邮寄地址" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea type="text" name="remark" placeholder="" class="layui-textarea"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="customAddFilter">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-normal">重置</button>
        </div>
    </div>
</form>
<!--行编辑工具栏-->
<script type="text/html" id="barRowEdit">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<!--头部工具栏-->
<script type="text/html" id="toolbarHead">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="delete">删除</button>
        <button class="layui-btn layui-btn-sm" lay-event="update">编辑</button>
    </div>
</script>
<!--JavaScript代码区域-->
<script src="/layui/layui.js"></script>
<script src="/echarts/jquery-3.4.1.min.js"></script>
<!--列表信息显示-->
<script>
    //自定义layui组件的目录
    layui.config({
        base: '/layui/lay/modules/'
    }).extend({
        //设定组件别名
        tableSelect: 'tableSelect',
    });
    layui.use(['table','layer','form','tableSelect','jquery'],function(){
        let table = layui.table;//数据表格模块
        let layer = layui.layer;//弹出层模块
        let form = layui.form;//表单模块
        let tableSelect = layui.tableSelect;//自定义的表格选择模块
        let $ = layui.jquery;

        //添加代理人选择表格
        tableSelect.render({
            elem: '#addUserTableSelect',	//定义输入框input对象 必填
            checkedKey: 'userId', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'userName',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '真实姓名搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '/user/queryAll',
                cols: [
                    [
                        {type: 'radio'}
                        , {field: 'userId', title: '编号', width: 80, sort: true}
                        , {field: 'loginName', title: '登录名', width: 100}
                        , {field: 'userName', title: '真实姓名', width: 120}
                    ]
                ]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...

                console.log(data.data)
                elem.val(data.data[0].userName)
                $("#addUserTableSelectHidden").val(data.data[0].userId)
            }
        })

        //修改代理人选择表格
        tableSelect.render({
            elem: '#updateUserTableSelect',	//定义输入框input对象 必填
            checkedKey: 'userId', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'userName',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '真实姓名搜索',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: '/user/queryAll',
                cols: [
                    [
                        {type: 'radio'}
                        , {field: 'userId', title: '编号', width: 80, sort: true}
                        , {field: 'loginName', title: '登录名', width: 100}
                        , {field: 'userName', title: '真实姓名', width: 120}
                    ]
                ]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                elem.val(data.data[0].userName)
                $("#updateUserTableSelectHidden").val(data.data[0].userId)
            }
        })
        //部门列表
        let customTable = table.render({
            elem: '#customTable' //table标签的id
            ,toolbar: '#toolbarHead'//开启头部工具栏，并为其绑定左侧模板
            ,url: '/custom/queryAll' //数据接口
            ,page: true //开启分页，默认向后台发两个参数page和limit
            // ,cellMinWidth: 80//自适应宽度，最短为80
            ,cols: [
                [ //表头 两个中括号不能在同一行，与thymeleaf语法冲突
                    {type: 'checkbox', fixed: 'left'} //开启复选框，必须有fixed
                    ,{field: 'customId', title: 'ID', sort: true, fixed: 'left', width: 80}
                    ,{field: 'contact', title: '联系人', sort: true, width: 90}
                    ,{field: 'phone', title: '手机号',sort: true, width: 120}
                    ,{field: 'email', title: '邮箱', sort: true, width: 120}
                    ,{field: 'applicant', title: '申请人', width: 80}
                    ,{field: 'customAddress', title: '邮寄地址',  sort: true, width: 160}
                    ,{field: 'createBy', title: '创建人',sort: true, width: 80}
                    ,{field: 'createTime', title: '创建时间', width: 160}
                    ,{field: 'updateBy', title: '修改人',sort: true, width: 80}
                    ,{field: 'updateTime', title: '修改时间', width: 160}
                    ,{field: 'proxyId', title: '代理人ID',hide: true, width: 80}
                    ,{field: 'userName', title: '代理人', width: 80}
                    ,{field: 'remark', title: '备注', width: 160},
                    {fixed: 'right', title:'操作',align:'center', toolbar: '#barRowEdit', width: 120}
                ]
            ]
        });
        //行编辑工具栏监听事件
        table.on('tool(customFilter)', function(obj){
            //获取当前行数据
            let data = obj.data;
            //判断是删除还是编辑
            if(obj.event === 'del'){
                layer.confirm('真的删除行么', function(index){
                    //向后台发送异步请求
                    $.ajax({
                        url:"/custom/deleteById",
                        type:"DELETE",
                        data:{customId:data.customId},
                        dataType:"json",
                        success:function (resp) {
                            if(resp.code==0){
                                layer.msg(resp.msg);
                                //重新加载数据
                                customTable.reload({
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                })
                            }
                        },
                        error:function (e) {
                            layer.msg("请求失败");
                        }
                    });
                });
            } else if(obj.event === 'edit'){
                //表单赋值，取值 这里是赋值
                form.val("customUpdateFilter",data);
                //打开弹出层
                layer.open({
                    type: 1,//指页面层
                    area:['670px', '550px'],
                    content: $('#customUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                });
            }
        });
        //头部工具栏监听事件
        table.on('toolbar(customFilter)', function(obj){
            let checkStatus = table.checkStatus(obj.config.id);
            let  checkData = checkStatus.data;
            switch(obj.event){
                case 'add':
                    //重置form表单
                    document.getElementById("customAddForm").reset();
                    layer.open({
                        type: 1,
                        area:['670px', '550px'],
                        content: $('#customAddForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
                case 'delete':
                    //判断是否选中行
                    if(checkData.length==0){
                        layer.msg('请至少选择一行数据');
                        return;
                    }
                    layer.confirm('真的删除行么', function(index) {
                        //获取所有选中行id
                        let customIds = "";
                        for (let i = 0; i < checkData.length; i++) {
                            customIds += checkData[i].customId + ",";
                        };
                        customIds = customIds.substr(0, customIds.length - 1);
                        layer.msg(customIds);
                        //调用批量删除
                        $.ajax({
                            url: "/custom/deleteByIds",
                            type: "DELETE",
                            data: {customIds: customIds},
                            dataType: "json",
                            success: function (resp) {
                                if (resp.code == 0) {
                                    layer.msg(resp.msg);
                                    //重新加载数据
                                    customTable.reload();
                                }
                            },
                            error: function (e) {
                                layer.msg("请求失败");
                            }
                        });
                    });
                    break;
                case 'update':

                    if(checkData.length!=1){
                        layer.msg('编辑只能选中一行');
                        return;
                    }
                    form.val("customUpdateFilter",checkData[0]);
                    layer.open({
                        type: 1,
                        area:['670px', '550px'],
                        content: $('#customUpdateForm') //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                    });
                    break;
            };
        });
        //更新(编辑)的监听事件
        form.on('submit(customUpdateFilter)', function(data){
            // layer.msg(JSON.stringify(data.field));
            // alert(JSON.stringify(data.field))
            $.ajax({
                url:"/custom/update",
                type:"PUT",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        customTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });
        //添加监听事件
        form.on('submit(customAddFilter)', function(data){
            $.ajax({
                url:"/custom/insert",
                type:"POST",
                data:data.field,
                dataType:"json",
                success:function (resp) {
                    if(resp.code==0){
                        layer.msg(resp.msg);
                        //关闭弹框
                        layer.closeAll('page');
                        //重新加载数据
                        customTable.reload();
                    }
                },
                error:function (e) {
                    layer.msg("请求失败");
                }
            });
            return false;
        });
        //顶部搜索框监听事件
        form.on('submit(searchFilter)', function (data) {
            //执行重载
            customTable.reload({
                where: {
                    contact: data.field.contact,
                    applicant: data.field.applicant,
                    updateBy: data.field.updateBy
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //重置条件查询表单，刷新table数据
        $("#myButton").click(function () {
            customTable.reload({
                where: { //设定异步数据接口的额外参数，任意设
                    contact: ""
                    , applicant: ""
                    , updateBy: ""
                }
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>
</body>
</html>